<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class M_iklan extends CI_Model {
	
    function __construct()
    {
        parent::__construct();
    }
    
    function get_iklan()
    {
        $this->db->select('*');
        $this->db->from('iklan_konten');
        $this->db->join('iklan_kategori','iklan_konten.ID_Kategori = iklan_kategori.ID_Kategori');
        $this->db->join('frum_meta','iklan_konten.user_id = frum_meta.user_id');
        $this->db->order_by('ID_Iklan','ASC');
        $params = $this->db->get();
        return $params->result();
    }
    
	function input_iklan()
	{  
	    $config['upload_path'] = './upload/iklan/';
	    $config['allowed_types'] = 'gif|jpg|png';
        $config['file_name']    = 'iklan_'.$this->input->post('id').'_gambar1';
        $this->load->library('upload', $config);
        
        $this->upload->do_upload('gambar1');
        
        $config['file_name']    = 'iklan_'.$this->input->post('id').'_gambar2'; 
        $this->upload->initialize($config);
        $this->upload->do_upload('gambar2');
        
        $config['file_name']    = 'iklan_'.$this->input->post('id').'_gambar3'; 
        $this->upload->initialize($config);
        $this->upload->do_upload('gambar3');
        
        
		$now = date("Y-m-d H-i-s");
		$data = array (
                'ID_Kategori'   => $this->input->post("kategori"),
                'Tanggal'       => $now,
                'Konten'        => $this->input->post("konten"),
                'Kontak'        => $this->input->post("kontak"),
                'Harga'         => $this->input->post("harga"),
                'Kondisi'       => $this->input->post("kondisi"),
                'Gambar_1'      => 'iklan_'.$this->input->post('id').'_gambar1',
                'Gambar_2'      => 'iklan_'.$this->input->post('id').'_gambar2',
                'Gambar_3'      => 'iklan_'.$this->input->post('id').'_gambar3',
                'user_id'       => '1'
                ); 
		return $this->db->insert("iklan_konten",$data);
	}
    
    function get_id_iklan($id)
    {
        $params = $this->db->get_where('iklan_konten', array('ID_iklan'=>$id));
        return $params->row();
    }
    
    function edit_iklan($id)
    {
        $config['upload_path'] = './upload/iklan/';
	    $config['allowed_types'] = 'gif|jpg|png';
        $config['overwrite'] = 'TRUE';
        $config['file_name']    = 'iklan_'.$this->input->post('id').'_gambar1';
        $this->load->library('upload', $config);
        
        $this->upload->do_upload('gambar1');
        
        $config['file_name']    = 'iklan_'.$this->input->post('id').'_gambar2'; 
        $this->upload->initialize($config);
        $this->upload->do_upload('gambar2');
        
        $config['file_name']    = 'iklan_'.$this->input->post('id').'_gambar3'; 
        $this->upload->initialize($config);
        $this->upload->do_upload('gambar3');
        
        $id = $this->input->post('idiklan');
        $edit_data =    array(
                        'ID_Kategori'   => $this->input->post('idkategori'),
                        'Konten'        => $this->input->post('konten'),
                        'Kontak'        => $this->input->post('kontak'),
                        'Harga'         => $this->input->post('harga'),
                        'Kondisi'       => $this->input->post('kondisi')
                        );
        $this->db->where('ID_Iklan',$id);
        $this->db->update('iklan_konten',$edit_data);   
    }
    
    function get_last_post()
    {
        $this->db->order_by('Tanggal','DESC');
        $this->db->limit(3);    
        $params = $this->db->get('iklan_konten');
        return $params->result();
    }
    function get_last_id()
    {
        $this->db->select('*');
        $this->db->from('iklan_konten');
        $this->db->order_by('ID_Iklan','DESC');
        $this->db->limit(1);
        $params = $this->db->get();
        return $params->row();
    }
}